var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        banner: [],
        page: 1,
        tid: 0,
        catName: '',
        cats: [],
        fid: 0,
        currentCatId: 0,
        catkey: -1,
        show_more: 0,


    },

    onShow: function (options) {
        // var cid = options.cid;
        // console.log(options);

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        var tid = options.tid;
        var cid = options.cid;
        this.setData({
            tid: tid,
            cid: cid,
            currentCatId: cid
        })
        app.globalSeo('news', cid).then(res => {
            // console.log(res);
            swan.setPageInfo({

                title: res.seo_title,
                keywords: res.seo_keywords,
                description: res.seo_description,
                articleTitle: res.seo_articleTitle,
                image: res.seo_img,
                // releaseDate: '2019-01-02 12:01:30',
            })

        })


        this.loadData(1);
        var that = this;
        let pages = getCurrentPages();
        var url = '/' + pages[pages.length - 1].uri;
        console.log(url);

        var sysinfo = swan.getStorageSync('info');
        app.head_backgroup(sysinfo);
        if (sysinfo == '') {
            app.globalSetting().then(res => {
                var custom_id = 0;
                for (let index in res.nav) {
                    var item = res.nav[index];

                    if ((item.url == url) || (item.url.indexOf('/pages/news/news?cid=' + cid) != -1)) {
                        app.globalData.currentNum = index;
                        custom_id = 1;
                        break;
                    }

                }
                if (custom_id == 0) {
                    app.globalData.currentNum = -1;
                }
                this.setData({
                    'info': res,
                    'currentNum': app.globalData.currentNum
                });
            });
        } else {
            // console.log('ss');
            var custom_id = 0;
            for (let index in sysinfo.nav) {
                var item = sysinfo.nav[index];

                if ((item.url == url) || (item.url.indexOf('/pages/news/news?cid=' + cid) != -1)) {
                    app.globalData.currentNum = index;
                    custom_id = 1;
                    break;
                }


            }
            if (custom_id == 0) {
                app.globalData.currentNum = -1;
            }

            console.log(app.globalData.currentNum);
            this.setData({
                'info': sysinfo,
                'currentNum': app.globalData.currentNum
            });



        }



        swan.request({
            url: app.util.url('entry/baiduapp/banner'),
            method: 'GET',
            dataType: 'json',
            data: {
                uniacid: app.siteInfo.uniacid,
                m: app.siteInfo.mName
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {

                that.setData({
                    'banner': res.data.data
                });

            }
        });


        // this.setTabBar(tid)
    },

    loadMore: function (event) {
        var id = event.currentTarget.dataset.page

        this.loadData(id);
    },

    catClick: function (event) {

        var cid = event.currentTarget.dataset.cid;
        var fid = event.currentTarget.dataset.fid;
        var catkey = event.currentTarget.dataset.catkey;

        // console.log(cid, catkey);
        if (this.data.catkey != catkey) {
            this.setData({
                cid: cid,
                // fid: fid,
                catkey: catkey,
                page: 1,
            })
            this.loadData(0);
        }
    },

    loadData: function (page) {
        var that = this
        var uniacid = app.siteInfo.uniacid;
        var cid = that.data.cid;
        swan.showLoading({
            title: "加载中……"
        })

        swan.request({
            url: app.util.url('entry/baiduapp/news'),
            method: 'GET',
            dataType: 'json',
            data: {
                page: page,
                uniacid: uniacid,
                cid: cid,
                version: 3302,
                m: app.siteInfo.mName
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                if (!res.data.errno) {

                    that.setData({
                        catName: res.data.data.catName,
                        cats: res.data.data.category,
                        show_more: res.data.data.show_more
                    })
                    var len = res.data.data.list.length;
                    var currentCatId = res.data.data.currentCatId;

                    if (!currentCatId) {
                        swan.setNavigationBarTitle({
                            title: '新闻资讯'
                        })
                    } else {
                        var catName = res.data.data.catName;

                        swan.setNavigationBarTitle({
                            title: catName + '资讯'
                        })
                    }



                    if (that.data.currentCatId != currentCatId) {
                        console.log(that.data.currentCatId + '---' + currentCatId);
                        that.setData({
                            list: [],
                            currentCatId: currentCatId
                        })
                    } else {
                        // console.log(1111);

                    }
                    if (len > 0) {
                        var page = that.data.page + 1;
                        that.setData({
                            page: page
                        })
                        var arr = that.data.list
                        // console.log(arr);
                        var newarr = arr.concat(res.data.data.list);
                        // console.log(newarr);
                        that.setData({
                            list: newarr
                        })
                        app.hide_loading();
                    } else {
                        app.hide_loading();
                    }

                }
            }
        });




    },

    onShareAppMessage: function (res) {
        // console.log(this.data);
        return {
            title: this.data.catName,
            path: '/pages/news/news?cid=' + this.data.cid
        }
    },
    tel(e) {
        app.tel(e.currentTarget.dataset.val);
    },
    http_tab(e) {

        app.tabNav(e);
    },
    http_navigator(e) {
        app.tonav(e);
    },


    driver: function () {
        swan.openLocation({
            latitude: Number(this.data.blist.jing),
            longitude: Number(this.data.blist.wei),
            address: this.data.blist.address
        })
    },
    tabNav: function (event) {
        var url = event.currentTarget.dataset.url;
        var tab = event.currentTarget.dataset.tab;
        if (url.indexOf('http') != '-1') {
            swan.setStorageSync('navurl', url)
            var url = '../webview/webview?op=' + tab
            swan.navigateTo({
                url: url,
            })
        } else {
            swan.redirectTo({
                url: url,
            })
        }
    },
    bannerAction(event) {

        var id = event.currentTarget.dataset.id;
        var cs = this.data.banner[id];
        if (cs.type_skip == 1) {
            app.bannerUrl(cs.url, cs.id);
        }
        if (cs.type_skip == 2) {
            app.globalData.currentNum = -1;
            app.bannerUrl(cs.url, cs.id);
        }
    },
    copy_action: function () {
        var copy = this.data.blist.copyright
        if (copy.copy_kind == '1') {
            var phone = copy.copy_phone
            swan.makePhoneCall({
                phoneNumber: phone,
            })
        }
        if (copy.copy_kind == '2') {
            var appid = copy.copy_appid;
            var pageaddress = copy.copy_ddress;
            swan.navigateToSmartProgram({
                appKey: appid,
                path: pageaddress,
                success(res) {
                }
            })

        }
    }
})